package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.activity.ImagePreviewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Holyn on 2017/7/25.
 */

public class ImgAdditionalActivity extends BaseActivity {
    @BindView(R.id.tv_empty_view)
    TextView tvEmptyView;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private ArrayList<String> sonImageUrlList;
    private MyRecyclerAdapter recyclerAdapter;

    public static void start(Activity fromActivity, ArrayList<String> sonImageUrlList){
        Intent intent = new Intent(fromActivity, ImgAdditionalActivity.class);
        intent.putStringArrayListExtra(SeniorDistinguishActivity.INTENT_KEY_SON_IMG_URL_LIST, sonImageUrlList);
        fromActivity.startActivity(intent);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_img_addition;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("近似图样");

        sonImageUrlList = getIntent().getStringArrayListExtra(SeniorDistinguishActivity.INTENT_KEY_SON_IMG_URL_LIST);

        if (sonImageUrlList.size() != 0){
            tvEmptyView.setVisibility(View.GONE);
            rvList.setVisibility(View.VISIBLE);

            recyclerAdapter = new MyRecyclerAdapter(sonImageUrlList);
            recyclerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                    ImagePreviewActivity.start(ImgAdditionalActivity.this, sonImageUrlList.get(i));
                }
            });
            rvList.setLayoutManager(new GridLayoutManager(ImgAdditionalActivity.this, 3));
            rvList.setAdapter(recyclerAdapter);
        }

    }

    private class MyRecyclerAdapter extends BaseQuickAdapter<String, BaseViewHolder>{

        public MyRecyclerAdapter(@Nullable List<String> data) {
            super(R.layout.item_img_addition, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, String s) {
            baseViewHolder.setText(R.id.tv_title, "子图"+(baseViewHolder.getAdapterPosition()+1)+"：");
            ImageView ivSon = baseViewHolder.getView(R.id.iv_son);
            Glide.with(ImgAdditionalActivity.this).load(s)
//                    .error(R.drawable.ic_empty_image)
                    .into(ivSon);
        }
    }
}
